package edu.whu.powersoap;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class JobListActivity extends Activity {

	private Context mContext;
	String username = MainActivity.user;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		mContext = JobListActivity.this;
		
		ContentResolver cr = mContext.getContentResolver();
		Cursor cusr = JobListActivity.this.getContentResolver().query(UserProvider.CONTENT_URI, new String[]
				{
					UserProvider.VOL_ID + " as _id",
					UserProvider.VOL_NAME,
					UserProvider.VOL_FLAG
				}, "name =?", new String[]{username}, null);
		cusr.moveToFirst();
		int id = cusr.getColumnIndex(UserProvider.VOL_FLAG);
		final String flag = cusr.getString(id);
		cusr.close();
		Cursor c;
		
		if(Integer.valueOf(flag) == 1){
			 c = cr.query(JobProvider.CONTENT_URI, new String[] 
					{
					JobProvider.VOLUMN_NUMBER + " as _id",
					JobProvider.VOLUMN_CREATE_TIME, 
					JobProvider.VOLUMN_EXECUTOR
					}, null, null, null);	
		}else{
			 c = cr.query(JobProvider.CONTENT_URI, new String[] 
					{
					JobProvider.VOLUMN_NUMBER + " as _id",
					JobProvider.VOLUMN_CREATE_TIME, 
					JobProvider.VOLUMN_EXECUTOR
					}, "executor =?", new String[]{username}, null);
		}
		
		MyCursorAdapter adapter = new MyCursorAdapter(this, c);
				
		setContentView(R.layout.job_list);

		ListView list = (ListView) findViewById(R.id.listView);
		list.setAdapter(adapter);
		
		list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Intent intent = new Intent(JobListActivity.this, JobDetailActivity.class);
                
                TextView number = (TextView) v.findViewById(R.id.text1);
                intent.putExtra("number", number.getText());
                startActivity(intent);
            }

        });
		
		super.onCreate(savedInstanceState);
	}

	class MyCursorAdapter extends CursorAdapter {

		public MyCursorAdapter(Context context, Cursor c) {
			super(context, c);
		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			LayoutInflater vi = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View view = vi.inflate(R.layout.job_line, parent, false);
			return view;
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			ViewGroup group = (ViewGroup) view;

			TextView number = (TextView) group.findViewById(R.id.text1);
			// Set the number
			number.setText(cursor.getString(cursor.getColumnIndex("_id")));

			TextView name = (TextView) group.findViewById(R.id.text2);
			// Set the name
			name.setText(cursor.getString(cursor
					.getColumnIndex(JobProvider.VOLUMN_EXECUTOR)));

		}

	}

}
